/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prepocessing_2.com.prep;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author baga
 */
public class SplitNormalizeToken {

    private static final char[] forbChar = {'.', ',', '+', '-', '^', '@', '#', '!',
        '$', '%', '&', '*', '(', ')', '-', '_', '=', '\\', '\'', '/', '\'',
        '\'', '?', '<', '>', ':', ';', '|'};

    public static String[] getNormalTokens(String tokens) {
        if (tokens != null) {
            //StemmerEng stem = new StemmerEng();
            String[] stringExpand = tokens.split(" ");

            int count = stringExpand.length;
            for (int i = 0; i < count; i++) {
                StringBuffer strBuf = new StringBuffer(stringExpand[i].toLowerCase());
                //System.out.print(new String(strBuf) + " ");
                StringBuffer strTemp = new StringBuffer(stringExpand[i].toLowerCase());
                int countStrBuf = strBuf.length();
                for (int j = 0; j < countStrBuf; j++) {
                    for (int x = 0; x < forbChar.length; x++) {
                        if (strBuf.charAt(j) == forbChar[x]) {
                            //strTemp = strBuf.deleteCharAt(x);
                            strTemp = strBuf.deleteCharAt(j);
                            //System.out.print(new String(strTemp) + " ");
                            countStrBuf--;
                            j--;
                            break;
                        }
                    }
                }
                stringExpand[i] = new String(strBuf);
                //String newString = new String(strBuf);
                //stem.add(newString.toCharArray(), newString.length());
                //stem.stem();
                //stringExpand[i] = stem.toString();
            }
            return stringExpand;
        } else {
            return null;
        }
    }

    public static String[] getNormalTokensStemmer(String tokens) {
        if (tokens != null) {
            StemmerEng stem = new StemmerEng();
            String[] stringExpand = tokens.split(" ");

            int count = stringExpand.length;
            for (int i = 0; i < count; i++) {
                StringBuffer strBuf = new StringBuffer(stringExpand[i].toLowerCase());
                //System.out.print(new String(strBuf) + " ");
                StringBuffer strTemp = new StringBuffer(stringExpand[i].toLowerCase());
                int countStrBuf = strBuf.length();
                for (int j = 0; j < countStrBuf; j++) {
                    for (int x = 0; x < forbChar.length; x++) {
                        if (strBuf.charAt(j) == forbChar[x]) {
                            //strTemp = strBuf.deleteCharAt(x);
                            strTemp = strBuf.deleteCharAt(j);
                            //System.out.print(new String(strTemp) + " ");
                            countStrBuf--;
                            j--;
                            break;
                        }
                    }
                }
                stringExpand[i] = new String(strBuf);
                String newString = new String(strBuf);
                stem.add(newString.toCharArray(), newString.length());
                stem.stem();
                stringExpand[i] = stem.toString();
                insertWord(stringExpand[i], 1);
            }
            return stringExpand;
        } else {
            return null;
        }
    }

    public static String[] getNormalTokensStopList(String tokens) {
        if (tokens != null) {
            //StemmerEng stem = new StemmerEng();
            String[] stringExpand = tokens.split(" ");

            int count = stringExpand.length;
            for (int i = 0; i < count; i++) {
                StringBuffer strBuf = new StringBuffer(stringExpand[i].toLowerCase());
                //System.out.print(new String(strBuf) + " ");
                StringBuffer strTemp = new StringBuffer(stringExpand[i].toLowerCase());
                int countStrBuf = strBuf.length();
                for (int j = 0; j < countStrBuf; j++) {
                    for (int x = 0; x < forbChar.length; x++) {
                        if (strBuf.charAt(j) == forbChar[x]) {
                            //strTemp = strBuf.deleteCharAt(x);
                            strTemp = strBuf.deleteCharAt(j);
                            //System.out.print(new String(strTemp) + " ");
                            countStrBuf--;
                            j--;
                            break;
                        }
                    }
                }
                stringExpand[i] = new String(strBuf);
                insertWord(stringExpand[i], 0);
                //String newString = new String(strBuf);
                //stem.add(newString.toCharArray(), newString.length());
                //stem.stem();
                //stringExpand[i] = stem.toString();
            }
            return stringExpand;
        } else {
            return null;
        }
    }

    private static void insertWord(String term, int opsi) {
        try {
            String tableOpsi = (opsi == 1 ? "words_count_stem" : "words_count");
            String url = "jdbc:mysql://localhost:3306/xsedb";
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url,
                    "root", "");
            String query = "select count(*) from " + tableOpsi + " where word = ?";
            PreparedStatement pst = conn.prepareStatement(query);
            pst.setString(1, term);
            ResultSet rs = pst.executeQuery();
            int wordCount = 0;
            while(rs.next()) {
                wordCount = rs.getInt(1);
            }
            pst.clearParameters();
            if (wordCount > 0) {
                query = "update " + tableOpsi + " set count = count + 1 where word = ?";
            } else {
                query = "insert into " + tableOpsi + " values(?, 1)";
            }
            pst = conn.prepareStatement(query);
            pst.setString(1, term);
            pst.execute();
            conn.close();
            conn = null;
            pst = null;
            rs = null;
        } catch (SQLException ex) {
            Logger.getLogger(SplitNormalizeToken.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ce) {

        }
    }

    public static void main(String[] args) {
        for(String x : getNormalTokens("saya seorang kapiten")) {
            System.out.println(x);
        }
    }
}
